<template>
	<div class="m_r">
		<header class="top_bar">
			<a onclick="window.history.go(-1)" class="icon_back"></a>
			<h3 class="cartname">商城注册</h3>
		</header>
		<main class="user_login_box">
			<div class="login_dialog">
				<div class="_username position_rel">
					<img class="login_accout_icon" src="../assets/images/account_icon.png"/>
					<input type="text" name="regname" placeholder="请输入手机号" class="user_input" v-model="regname" style="padding-left: 40px;">
				</div>
				<div class="_username u_passwd position_rel">
					<img class="login_accout_icon" src="../assets/images/password_icon.png"/>
					<input type="text" name="regpasswd" placeholder="请输入验证码" class="user_input" v-model="Code" style="padding-left: 40px;">
					<input :disabled="disabled" type="button" id="btn" @click="getCode" value="免费获取验证码" class="get_code_btn">
				</div>
				<div class="_username u_passwd position_rel">
					<img class="login_accout_icon" src="../assets/images/password_icon.png"/>
					<input type="password" name="regpasswd" placeholder="请输入密码" class="user_input" v-model="regpasswd" style="padding-left: 40px;">
				</div>
				<div class="_username u_passwd position_rel">
					<img class="login_accout_icon" src="../assets/images/password_icon.png"/>
					<input type="password" name="regpasswd_ag" placeholder="请再次输入密码" class="user_input" v-model="regpasswd_ag" style="padding-left: 40px;">
				</div>
				<div class="login_box">
					<a @click="goSearch()" class="btn_login">注册</a>
				</div>
			</div>
		</main>
	</div>
</template>
<script>
	import { Toast } from 'mint-ui';
	export default {
		data() {
			return {
				regname: '',
				regpasswd: '',
				regpasswd_ag: '',
				regInfo: {},
				disabled: false,
				Code: ''
			}
		},
		methods: {
			goSearch() {
				let _this = this;
				if(_this.regpasswd != _this.regpasswd_ag) {
					Toast('两次输入密码不一致！')
					return false;
				}
				_this.$http.post('/base/signUp', {
					CellPhone: _this.regname,
					Password: _this.regpasswd,
					code: _this.Code
				}, localStorage.Authorization, res => {
					console.log('注册返回res', res)
					if(res.code === 200) {
						Toast(res.data);
						setTimeout(function() {
							_this.$router.push({
								path: '/login'
							});
						}, 1500)

					} else {
						Toast(res.error);

					}
				})

			},
			getCode: function() {
				let _this = this
				if(_this.regname==='') {
					Toast('请输入手机号！')
					return false
				}
				_this.$http.get('/base/SmsCode', {
					CellPhone: _this.regname,
				}, localStorage.Authorization, r => {
					var countdown = 60;

					function sendemail() {
						var obj = document.getElementById("btn");
						settime(obj);
					}

					function settime(obj) { //发送验证码倒计时
						if(countdown == 0) {
							_this.disabled = false;
							//obj.removeattr("disabled");
							obj.value = "免费获取验证码";
							countdown = 60;
							return;
						} else {
							_this.disabled = true;
							
//							obj.setAttribute('disabled', true);
							obj.value = "重新发送(" + countdown + ")";
							countdown--;
						}
						setTimeout(function() {
							settime(obj)
						}, 1000)
					}
					sendemail()
				})
			},
		}
	}
</script>
<style>
	@import '../assets/css/login.css';
	@import '../assets/css/reg.css';
</style>